package com.gump.controller;

import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.*;
import com.jfinal.weixin.sdk.jfinal.ApiController;

import java.util.logging.Logger;

public class WeixinApiController extends ApiController {

    static Log logger = Log.getLog(WeixinApiController.class);

    /**
     * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
     * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
     */
    public ApiConfig getApiConfig() {
        ApiConfig ac = new ApiConfig();

        // 配置微信 API 相关常量
        ac.setToken(PropKit.get("token"));
        ac.setAppId(PropKit.get("appId"));
        ac.setAppSecret(PropKit.get("appSecret"));

        /**
         *  是否对消息进行加密，对应于微信平台的消息加解密方式：
         *  1：true进行加密且必须配置 encodingAesKey
         *  2：false采用明文模式，同时也支持混合模式
         */
        ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
        ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
        return ac;
    }

    /**
     * 获取公众号菜单
     */
    public void getMenu() {
        ApiResult apiResult = MenuApi.getMenu();
        if (apiResult.isSucceed())
            renderText(apiResult.getJson());
        else
            renderText(apiResult.getErrorMsg());
    }

    /**
     * 创建菜单
     */
    public void createMenu()
    {
        String str = "{\n" +
                "    \"button\": [\n" +
                "        {\n" +
                "            \"name\": \"进入理财\",\n" +
                "            \"url\": \"http://m.bajie8.com/bajie/enter\",\n" +
                "            \"type\": \"view\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"安全保障\",\n" +
                "            \"key\": \"112\",\n" +
                "\t    \"type\": \"click\"\n" +
                "        },\n" +
                "        {\n" +
                "\t    \"name\": \"使用帮助\",\n" +
                "\t    \"url\": \"http://m.bajie8.com/footer/cjwt\",\n" +
                "\t    \"type\": \"view\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        ApiResult apiResult = MenuApi.createMenu(str);
        if (apiResult.isSucceed())
            renderText(apiResult.getJson());
        else
            renderText(apiResult.getErrorMsg());
    }

    /**
     * 获取公众号关注用户
     */
    public void getFollowers()
    {
        ApiResult apiResult = UserApi.getFollows();
        renderText(apiResult.getJson());
    }

    /**
     * 获取用户信息
     */
    public void getUserInfo()
    {
        ApiResult apiResult = UserApi.getUserInfo("ohbweuNYB_heu_buiBWZtwgi4xzU");
        renderText(apiResult.getJson());
    }

    /**
     * 发送模板消息
     */
    public void sendMsg()
    {
        String openId = getPara("openId");
        String name = getPara("name");
        String date = getPara("date");
        String price = getPara("price");
        String orderId = getPara("orderId");
        String machineId = getPara("machineId");
        String code = getPara("code");

        String str = " {\n" +
                "           \"touser\":\"" + openId + "\",\n" +
                "           \"template_id\":\"vxPEDquuXAJEDAl1XvNrqa3H0DpD8Bhu45fEjLfzVHs\",\n" +
                "           \"url\":\"http://gump.carp.mopaasapp.com/order\",\n" +
                "           \"topcolor\":\"#FF0000\",\n" +
                "           \"data\":{\n" +
                "                   \"first\":{\n" +
                "                       \"value\":\"购买成功，猛戳此消息 〜领礼券〜\\n\",\n" +
                "                       \"color\":\"#3889F9\"\n" +
                "                   },\n" +
                "                   \"keyword1\":{\n" +
                "                       \"value\":\"" + orderId + "\",\n" +
                "                       \"color\":\"#000000\"\n" +
                "                   },\n" +
                "                   \"keyword2\":{\n" +
                "                       \"value\":\"" + name + "\",\n" +
                "                       \"color\":\"#3889F9\"\n" +
                "                   },\n" +
                "                   \"keyword3\":{\n" +
                "                       \"value\":\"" + price + "元\",\n" +
                "                       \"color\":\"#3889F9\"\n" +
                "                   },\n" +
                "                   \"keyword4\":{\n" +
                "                       \"value\":\"" + date + "\",\n" +
                "                       \"color\":\"#000000\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"售货机编号：" + machineId + "\\n取货码：" + code + "\\n\\n有任何问题请公众号直接留言或call我 400-839-7770\",\n" +
                "                       \"color\":\"#000000\"\n" +
                "                   }\n" +
                "           }\n" +
                "       }";
        ApiResult apiResult = TemplateMsgApi.send(str);
        renderText(apiResult.getJson());
    }

    /**
     * 获取参数二维码
     */
    public void getQrcode()
    {
        String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
        ApiResult apiResult = QrcodeApi.create(str);
        renderText(apiResult.getJson());

//        String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"123\"}}}";
//        ApiResult apiResult = QrcodeApi.create(str);
//        renderText(apiResult.getJson());
    }

    /**
     * 长链接转成短链接
     */
    public void getShorturl()
    {
        String str = "{\"action\":\"long2short\"," +
                "\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}";
        ApiResult apiResult = ShorturlApi.getShorturl(str);
        renderText(apiResult.getJson());
    }

    /**
     * 获取客服聊天记录
     */
    public void getRecord()
    {
        String str = "{\n" +
                "    \"endtime\" : 987654321,\n" +
                "    \"pageindex\" : 1,\n" +
                "    \"pagesize\" : 10,\n" +
                "    \"starttime\" : 123456789\n" +
                " }";
        ApiResult apiResult = CustomServiceApi.getRecord(str);
        renderText(apiResult.getJson());
    }

    /**
     * 获取微信服务器IP地址
     */
    public void getCallbackIp()
    {
        ApiResult apiResult = CallbackIpApi.getCallbackIp();
        renderText(apiResult.getJson());
    }

    public void createCard()
    {
        String openId = getPara("openId");
        String name = getPara("name");
        String date = getPara("date");
        String price = getPara("price");
        String orderId = getPara("orderId");
        String machineId = getPara("machineId");
        String code = getPara("code");

        String str = "{\"card\":{\"card_type\":\"DISCOUNT\",\"discount\":{\"base_info\":{\"logo_url\":\"http://mmbiz.qpic.cn/mmbiz/DZVgT7OCTCIsc0RibJMKCiaREdsUSAomhuNeEI9LXH8YSkO3xGSvaA5005JgtdEFBgdD7MeGCMTMxZa4Au7icPIug/0\",\"brand_name\":\"博强的商铺\",\"code_type\":\"CODE_TYPE_TEXT\",\"title\":\"七折折扣券\",\"sub_title\":\"七折！七折！七折！\",\"color\":\"Color010\",\"notice\":\"使用时向服务员出示此券\",\"service_phone\":\"020-88888888\",\"description\":\"不可与其他优惠同享\\n如需团购券发票，请在消费时向商户提出\\n店内均可使用，仅限堂食\",\"date_info\":{\"type\":\"DATE_TYPE_FIX_TIME_RANGE\",\"begin_timestamp\":\"1454394816\",\"end_timestamp\":\"1954394816\"},\"sku\":{\"quantity\":500000},\"get_limit\":3,\"use_custom_code\":false,\"bind_openid\":false,\"can_share\":true,\"can_give_friend\":true,\"location_id_list\":[123,12321,345345],\"custom_url_name\":\"立即使用\",\"custom_url\":\"http://www.baidu.com\",\"custom_url_sub_title\":\"不要点这里\",\"promotion_url_name\":\"更多优惠\",\"promotion_url\":\"http://www.baidu.com\",\"source\":\"大众点评\"},\"discount\":30}}}";
        ApiResult apiResult = CardApi.create(str);
        renderText(apiResult.getJson());
    }
}